package cn.edu.ctbu.lddweb.controller;

import cn.edu.ctbu.lddweb.pojo.Semester;
import cn.edu.ctbu.lddweb.service.SemesterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 学期管理Controller
 */
@Controller
public class SemesterController {
    @Autowired
    private SemesterService semesterService;

    //后台管理系统
    @RequestMapping("/semester/index")
    public String index(Model model, ModelMap map,Semester semester){
        List<Semester> semesters = semesterService.selectList(semester);
        model.addAttribute("semester",semesters);
        return "/semester/index";
    }

    //搜索/重置
    @RequestMapping("/selectSemester")
    public String selectCourse(@RequestParam(value = "semesterName",required = false) String semesterName, Model model){
        Semester semester = new Semester();
        semester.setSemesterName(semesterName);
        List<Semester> list = semesterService.selectList(semester);
        model.addAttribute("semester",list);
        return "/semester/index";
    }

    //新增页面
    @RequestMapping("/semester/insert")
    public String insert(){
        return "/semester/insert";
    }

    //新增操作
    @RequestMapping("/insertSemester")
    public String insertSemester(Semester semester){
        //判断是否重复
        List<Semester> list = semesterService.selectList(semester);
        if(list.size()!=0){
            return "error111";
        }
        semesterService.insertSemester(semester);
        return "/semester/index";
    }


    //修改
    @RequestMapping("/semester/edit")
    public String edit(int id,Model model){
        Semester semester = semesterService.findID(id);
        model.addAttribute("semester",semester);
        return "/semester/edit";
    }

    //修改操作
    @RequestMapping("/updateSemester")
    public String updateSemester(Semester semester){
        Boolean boo = semesterService.checkSemesterUnique(semester);
        if(boo){
            return "error111";
        }
        semesterService.updateSemester(semester);
        return "/semester/index";
    }

    //删除
    @RequestMapping("/deleteSemester")
    public String deleteSemester(int id){
        semesterService.deleteSemester(String.valueOf(id));
        return "/semester/index";
    }
}
